<template>
	<view class="page">
		<view class="top-stats">
			<view class="top-stats-item">
				<view class="title">{{ statsInfo.allOrders }}</view>
				<view class="value">全部订单</view>
			</view>
			<view class="top-stats-item">
				<view class="title">{{ statsInfo.pendingServiceOrder }}</view>
				<view class="value">待服务订单</view>
			</view>
			<view class="top-stats-item">
				<view class="title">{{ statsInfo.completedOrder }}</view>
				<view class="value">已完成订单</view>
			</view>
			<view class="top-stats-item">
				<view class="title">{{ statsInfo.orderToday }}</view>
				<view class="value">今日订单</view>
			</view>
			<view class="top-stats-item">
				<view class="title">{{ statsInfo.toBeServedToday }}</view>
				<view class="value">今日待服务</view>
			</view>
			<view class="top-stats-item">
				<view class="title">{{ statsInfo.completedToday }}</view>
				<view class="value">今天已完成</view>
			</view>
		</view>
		<view class="time-piker">
			<view class="right-info" @click="chooseTime">
				<view class="time">{{ params.queryDate }}</view>
				<image class="icon" src="/static/images/calendard@2x.png"></image>
			</view>
		</view>
		<view class="order-list">
			<view class="order-item" v-for="(item, index) in orderList" :key="index">
				<view class="top-info">
					<view class="type">{{ item.serviceName }}</view>
					<view class="state" style="color: #32C59A;">{{ item.status | statusFilter }}</view>
				</view>
				<view class="serve-time" v-if="item.patientName">
					就诊人：
					<text>{{ item.patientName }}</text>
				</view>
				<view class="serve-user">
					就诊医院：
					<text>{{ item.hospitalName }}</text>
				</view>
				<view class="serve-user" v-if="item.treatmentTime">
					服务时间：
					<text>{{ item.treatmentTime }}</text>
				</view>
				<view class="serve-user">
					服务需求：
					<text>{{ item.serviceRequirements }}</text>
				</view>
				<view class="bottom-info">
					<view class="cancel-pay" @click="toDetail(item)">查看详情</view>
					<view class="cancel-pay" v-if="item.status == 2" @click="goPage(item)">服务完成</view>
				</view>
			</view>
		</view>
		<!-- 年月日选择 -->
		<u-datetime-picker :show="show" v-model="dateTime" mode="date" @cancel="close()" @close="close" @confirm="confirm" :closeOnClickOverlay="true"></u-datetime-picker>
	</view>
</template>

<script>
import { getNowFormatDate } from '@/utils/index.js';
import { getEscortOrderStatistics, getOrderPageList } from '@/apis/request.js';

export default {
	data() {
		return {
			show: false,
			dateTime: Number(new Date()),
			statsInfo: '',
			params: {
				pageNo: 1,
				pageSize: 10,
				queryDate: '',
				status: '', //状态 0待付款 1待服务 2服务中 3服务完成 90退款中 91已退款 99已取消
				type: 1 //类型 0用户查询 1陪诊师查询
			},
			orderList: []
		};
	},
	filters: {
		statusFilter(status) {
			if (status == 0) {
				return '待付款';
			} else if (status == 1) {
				return '待服务';
			} else if (status == 2) {
				return '服务中';
			} else if (status == 3) {
				return '服务完成';
			} else if (status == 90) {
				return '退款中';
			} else if (status == 91) {
				return '已退款';
			} else if (status == 99) {
				return '已退款';
			}
		}
	},
	onLoad() {
		this.params.queryDate = getNowFormatDate(new Date());
		this.getStatsInfo();
		this.getList();
	},
	onPullDownRefresh() {
		this.params.pageNo = 1;
		this.orderList = [];
		this.getList();
		uni.stopPullDownRefresh();
	},
	onReachBottom() {
		this.params.pageNo++;
		this.getList();
	},
	onShow() {
		uni.$on('getOrderPageList', () => {
			this.getList();
		});
		uni.$on('serviceComplete', () => {
			this.getList();
		});
	},
	methods: {
		chooseTime() {
			this.show = true;
		},
		close() {
			this.show = false;
		},
		confirm(e) {
			this.show = false;
			this.params.queryDate = getNowFormatDate(e.value);
			this.params.pageNo = 1;
			this.getList();
		},
		//服务完成跳转
		goPage(item) {
			uni.navigateTo({
				url: '/pages/attendantPages/serviceComplete?orderId=' + item.id
			});
		},
		//查看详情
		toDetail(item) {
			uni.navigateTo({
				url: `/pages/attendantPages/orderDetails?orderId=${item.id}`
			});
		},
		getStatsInfo() {
			getEscortOrderStatistics().then(res => {
				this.statsInfo = res;
			});
		},
		getList() {
			getOrderPageList(this.params).then(res => {
				if (this.params.pageNo == 1) {
					this.orderList = res.records;
				} else {
					if (this.orderList.length < res.total) {
						this.orderList = this.orderList.concat(res.records);
					} else {
						this.orderList = this.orderList;
					}
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.page {
	background-color: #f2f2f2ff;
	height: 100%;
	min-height: 100vh;

	.top-stats {
		background-color: #32C59A;
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		text-align: center;
		padding: 20rpx 0;

		.top-stats-item {
			padding: 24rpx 0;
			width: 250rpx;

			.title {
				font-size: 40rpx;
				color: #fff;
			}

			.value {
				margin-top: 10rpx;
				font-size: 24rpx;
				color: #a1c6f4;
			}
		}
	}

	.order-list {
		view:first-child {
			margin-top: 0rpx;
		}

		.order-item {
			margin-top: 20rpx;
			background-color: #fff;
			width: 750rpx;
			box-sizing: border-box;
			padding: 30rpx 30rpx 20rpx 30rpx;

			.top-info {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.type {
					font-size: 30rpx;
				}

				.state {
					font-size: 28rpx;
				}
			}

			.serve-time {
				margin-top: 20rpx;
				font-size: 24rpx;
				color: #999999;

				text {
					color: #020202;
				}
			}

			.serve-user {
				margin: 14rpx 0 26rpx 0;
				font-size: 24rpx;
				color: #999999;

				text {
					color: #020202;
				}
			}

			.bottom-info {
				border-top: 2rpx solid #ececec;
				padding-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.pay-time {
					font-size: 22rpx;
					color: #020202;

					text {
						color: #ff290b;
					}
				}

				.cancel-pay {
					margin-left: 20rpx;
					width: 150rpx;
					height: 50rpx;
					line-height: 50rpx;
					border-radius: 25rpx;
					color: #999999;
					font-size: 26rpx;
					text-align: center;
					border: 2rpx solid #dddddd;
				}
			}
		}
	}

	.time-piker {
		height: 120rpx;
		width: 750rpx;
		background-color: #fff;
		position: relative;
		border-bottom: 2rpx solid #f0f0f0;

		.right-info {
			position: absolute;
			display: flex;
			align-items: center;
			padding: 14rpx 16rpx;
			border: 2rpx solid #e8e8e8;
			right: 40rpx;
			top: 30rpx;
			border-radius: 12rpx;

			.time {
				font-size: 28rpx;
				margin-right: 10rpx;
			}

			.icon {
				width: 34rpx;
				height: 32rpx;
			}
		}
	}
}
</style>
